package com.edward.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import com.edward.JsonDateProcessor;
import com.edward.JsonTimeProcessor;
import com.edward.PagerModel;
import com.edward.model.ActHarvest;
import com.edward.model.OtherHarvest;
import com.edward.model.SelfHarvest;
import com.edward.service.ActHarvestManager;
import com.edward.service.OtherHarvestManager;
import com.edward.service.SelfHarvestManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class HarvestAction extends ActionSupport{


	private static final long serialVersionUID = 4912812398720905688L;

	private SelfHarvest selfHarvest;
	
	private ActHarvest actHarvest;

	private OtherHarvest otherHarvest;
	
	private SelfHarvestManager selfHarvestManager;
	
	private ActHarvestManager actHarvestManager;
	
	private OtherHarvestManager otherHarvestManager;

	private String query="";
	
	private String sd;
	
	private int limit;
	
	private int start;
	
	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public String getSd() {
		return sd;
	}

	public void setSd(String sd) {
		this.sd = sd;
	}

	public SelfHarvest getSelfHarvest() {
		return selfHarvest;
	}
	
	public void setSelfHarvest(SelfHarvest selfHarvest) {
		this.selfHarvest = selfHarvest;
	}

	public ActHarvest getActHarvest() {
		return actHarvest;
	}

	public void setActHarvest(ActHarvest actHarvest) {
		this.actHarvest = actHarvest;
	}
	

	public OtherHarvest getOtherHarvest() {
		return otherHarvest;
	}

	public void setOtherHarvest(OtherHarvest otherHarvest) {
		this.otherHarvest = otherHarvest;
	}

	public void setOtherHarvestManager(OtherHarvestManager otherHarvestManager) {
		this.otherHarvestManager = otherHarvestManager;
	}

	public void setActHarvestManager(ActHarvestManager actHarvestManager) {
		this.actHarvestManager = actHarvestManager;
	}

	public void setSelfHarvestManager(SelfHarvestManager selfHarvestManager) {
		this.selfHarvestManager = selfHarvestManager;
	}

	@Secured("ROLE_COREFINANCE_ADD")
	public String addSelf() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		selfHarvestManager.addSelf(selfHarvest);
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}

	@Secured("ROLE_COREFINANCE_UPDATE")
	public String updateSelf() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		SelfHarvest s  = selfHarvest;
		SelfHarvest sh = selfHarvestManager.getSelfHarvest(s.getId());
		s.setIncome(sh.getIncome());
		s.setFee(sh.getFee());
		s.setTax(sh.getTax());
		s.setTag("t");
		selfHarvest.setNetProfit(s.getIncome() - s.getCost() - s.getFee() - s.getTax() + s.getBalance() - s.getOther() - s.getLoss());
		selfHarvest.setGrossProfit(s.getIncome()- s.getCost());
		if(s.getNetProfit()==0||s.getIncome()==0){
			selfHarvest.setRate(0);
		}else{
			selfHarvest.setRate(s.getNetProfit()/s.getIncome()*100);
			int i = 0;
			double d ;
			i = (int) (selfHarvest.getRate() * 1000);
			d = i;
			selfHarvest.setRate(d/1000);
		}
		selfHarvestManager.updateSelfSum(selfHarvest);
		selfHarvestManager.updateSelf(selfHarvest);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	
	@Secured("ROLE_COREFINANCE_UPDATE")
	public String deleteSelf() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		selfHarvestManager.deleteSelf(selfHarvest.getId());
		response.getWriter().print("{success : true , msg : '删除成功!'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE")
	public String getSelfList() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
		    JsonConfig cfg = new JsonConfig();
			cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
			cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
			PagerModel pager;
		    pager = selfHarvestManager.pagerList(start, limit,query);
			JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
			String jsonStr = jsonObject.toString();
			response.getWriter().print(jsonStr);
			return null;
		}
	
	@Secured("ROLE_COREFINANCE")
	public String getSelf() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
		    JsonConfig cfg = new JsonConfig();
			cfg.registerJsonValueProcessor(java.util.Date.class, new JsonTimeProcessor());
			cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonTimeProcessor());
			PagerModel pager;
		    pager = selfHarvestManager.pagerSelf(start, limit,selfHarvest);
			JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
			String jsonStr = jsonObject.toString();
			response.getWriter().print(jsonStr);
			return null;
		}
	
	@Secured("ROLE_COREFINANCE_ADD")
	public String addAct() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		DateFormat format = new SimpleDateFormat("yyyy-MM"); 
		String t = actHarvestManager.addAct(format.parse(sd));
		response.getWriter().print("{success : true , msg : '" + t + "'}");
		return null;
	}

	@Secured("ROLE_COREFINANCE_ADD")
	public String updateAct() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		DateFormat format = new SimpleDateFormat("yyyy-MM"); 
		ActHarvest a  = actHarvest;
		actHarvest.setDate(format.parse(sd));  
		actHarvest.setNetProfit(a.getActRate() + a.getDiffPrice());
		actHarvestManager.updateAct(actHarvest);
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}	
	
	@Secured("ROLE_COREFINANCE")
	public String getAct() throws Exception {
	
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
		    JsonConfig cfg = new JsonConfig();
			cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
			cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
			PagerModel pager;
		    pager = actHarvestManager.pagerList(start, limit,query);
			JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
			String jsonStr = jsonObject.toString();
			response.getWriter().print(jsonStr);
			return null;
		}
	
	@Secured("ROLE_COREFINANCE_ADD")
	public String addOther() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		DateFormat format = new SimpleDateFormat("yyyy-MM"); 
		String t = otherHarvestManager.addOther(format.parse(sd));
		response.getWriter().print("{success : true , msg : '" + t + "'}");
		return null;
	}

	@Secured("ROLE_COREFINANCE_ADD")
	public String updateOther() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		DateFormat format = new SimpleDateFormat("yyyy-MM"); 
		otherHarvest.setDate(format.parse(sd));  
		otherHarvestManager.updateOther(otherHarvest);
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}	
	
	@Secured("ROLE_COREFINANCE")
	public String getOther() throws Exception {
	
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
		    JsonConfig cfg = new JsonConfig();
			cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
			cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
			PagerModel pager;
		    pager = otherHarvestManager.pagerList(start, limit,query);
			JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
			String jsonStr = jsonObject.toString();
			response.getWriter().print(jsonStr);
			return null;
		}
	
	@Secured("ROLE_COREFINANCE")
	public String getSum() throws Exception {
		
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
	    pager = selfHarvestManager.pagerSum(start, limit,query);
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}

	
	@Secured("ROLE_COREFINANCE")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
	
	
	
	
	
	
	

}
